jQuery(function($){
    //

});

//.checked 
function tree_check(elems_a)
{
    $elems_li = $(elems_a).parent('li');
    if($(elems_a).parents('div.tree-servent').length == 0){
        the_class = 'checked' ;
    }else{
        the_class = 'to_be_removed';
    }
    
    if(typeof can_select_multi != 'undefined' && can_select_multi){
        $elems_li.toggleClass( the_class );
        if($elems_li.hasClass(the_class)){
            $elems_li.find('li').addClass(the_class);
        }else{
            $elems_li.find('li').removeClass(the_class);
        }
    }else{
        
        if(!$elems_li.hasClass('last-level')){
            //console.log($elems_li.get(0));
            return false;
        }
        $hidden = $elems_li.parents('.tree:eq(0)').prev('div.tree-up-lable').children('input[type=hidden]');
        $hidden.val( $elems_li.attr('value') );
    
        $hidden = $elems_li.parents('.tree:eq(0)').prev('div.tree-up-lable').children('input[type=hidden]');

        $all_leaf_elems = $( 'li.last-level' );

        $all_leaf_elems.removeClass( the_class );
        $elems_li.addClass( the_class );

        $hidden.val( $elems_li.attr('value') );
    
    }
    return false;
}
/*

function tree_check(elems_li)
{
    $elems_li = $(elems_li);
    $hidden = $elems_li.parents('.tree:eq(0)').prev('div.tree-up-lable').children('input[type=hidden]');

    $all_leaf_elems = $( 'li.last-level' );

    $all_leaf_elems.removeClass( 'checked' );
    $elems_li.addClass( 'checked' );

    $hidden.val( $elems_li.attr('value') );

    return false;
}
*/

//.checked 
function tree_copy(  )
{
    
}

function clear_servent(tree_id){
    $servet = $( '#' + tree_id + '_servent ul.ul-root' ).empty();
    $('.tree-servent select,.tree-servent input').val('').empty();
    return false;
}


function remove_from_tree(tree_id){
    
    
    $field        = $servet.parent('div.tree-servent').find( '.html_holder' );
    //console.log( $servet.find('.to_be_removed') );
    
    
    $servet.find('.to_be_removed').each(function(){
        $this = $(this);
        model = $this.attr('id').match(/([a-zA-Z_])*/)[0];
        model = model.substr(0 ,model.length-1);
        //console.log(model);
        $field_model_option = $servet.parent('div.tree-servent').children( 'select.models_holder[model="'+model+'"]').children('option[value="'+$this.attr('value')+'"]' );
        $field_model_option.remove();
        $this.remove();
    });
    
    $field.val($servet.html());
    return false;
    
}
function add_to_tree(tree_id){
    $tree   = $( '#tree_' + tree_id );
    //console.log($tree);
    $servet = $( '#' + tree_id + '_servent ul.ul-root' );
    //console.log($servet);
    $to_copy_html = "";
    $tree.find('li.checked').each(function(){
        $to_copy_html += get_html_with_parents($(this));
    });
    
    $servet.html( $to_copy_html );
    $servet.find('li').each( function( ) {
        this.id = this.id + '_servent' ;
        $(this).attr( 'parent' , $(this).attr('parent') + '_servent' ) ;
    });
    
    $servet.find('li').each(function(){
        $this = $(this);
        if( !$this.hasClass('checked') && $this.find('li.checked').length == 0 ){
           $this.remove();
        }
        
    });
    
    $servet.find('li').each(function(){
        $this = $(this);
        if($servet.find( 'li#'+$this.attr('id') ).length > 1){
            $this.remove();
            //$this.css('color','red !important')
        }
    });



    $field        = $servet.parent('div.tree-servent').find( 'input.html_holder' );
    //console.log( $field );
    $field_models = $servet.parent('div.tree-servent').children( 'select.models_holder' );
    $field_models.empty();
    //console.log($field);
    $field.val( $servet.html() );
    //console.log($field_models);
    
    
    $field_models.each(function(i ,model_obj){
        model = $(model_obj).attr('model');
        //console.log(model);
        //console.log($servet.find('li[id*='+model+']'));
        $servet.find('li[id*='+model+']').each(function(){
            //console.log(model);
            //console.log(model_obj);
            $(model_obj).append('<option value="'+$(this).attr('value')+'" selected="selected">'+$(this).attr('value')+'</option>');
        });
    });
    
    
    
    return false;

    
}

//var tree_items_arr = new Array();
function get_html_with_parents ( $li ) {
    
    $parent = $li;
    
    //console.log($li.attr('parent'));
    is_parent = true;
    
    while(true){
        //console.log( typeof $li.attr('parent') );
        if( !$li.attr('parent') || typeof $li.attr('parent') == 'undefined' ){
            break;
        }
        $li = $parent = $( '#' + $li.attr('parent') );
        is_parent = false;
    }
    
    //return '<li>'+$parent.html()+'</li>';
    if(is_parent || 1){
        return '<li value="'+$parent.attr('value')+'"  struct="'+$parent.attr('struct')+'" id="'+$parent.attr('id')+'" class="'+$parent.attr('class')+'">'+$parent.html()+'</li>';
        
    }else{
        return $parent.html( );
    }

}












